package com.example.it.adapter;

import android.content.Context;
import android.graphics.Canvas;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.it.R;
import com.example.it.bean.DataBean;

import java.util.List;

public class Adpater extends RecyclerView.Adapter<Adpater.ViewHolder> {
    private Context context;
    private List<DataBean> datas;

    public Adpater(Context context, List<DataBean> datas) {
        this.context = context;
        this.datas = datas;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.item1, parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        DataBean dataBean = datas.get(position);
        holder.name.setText(dataBean.getName());
        holder.aSwitch.setChecked(dataBean.isSelect());
        if (holder.aSwitch.isChecked()==false){
            dataBean.setSelect(false);
        }else if (holder.aSwitch.isChecked()==true){
            dataBean.setSelect(true);
        }
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView name;
        Switch aSwitch;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            aSwitch=itemView.findViewById(R.id.switch1);
            name=itemView.findViewById(R.id.textView);
        }
    }
}
